package org.cwnuit.train.utils;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class SelectTagValue extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String name;  //select name值 

	private String id;	  //select id 值 
	
	private String currentValue;	//select被选中的值 
	
	private Map<String,String> data; // select数据
	
	@Override
	public int doEndTag() throws JspException {
		
		//输出select html
		JspWriter out = pageContext.getOut();
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("<select name='");
			sb.append(name);
			sb.append("' id='");
			sb.append(id);
			sb.append("'>");
		
			if(data!=null)
			{ 
				Iterator<String> keys = data.keySet().iterator();
				 while(keys.hasNext())
				 {
					 String distxt = keys.next(); //显示 的文本
					 String value =data.get(distxt); //值
					 sb.append("<option value='");
					 sb.append(value);
					 sb.append("'");
					 if(currentValue!=null && currentValue.equals(value))
					 {
						 sb.append(" selected");
					 }
					 sb.append(">");
					 sb.append(distxt);
					 sb.append("</option>");
				 }
			}
			sb.append("</select>");
			out.print(sb.toString());
		} catch (IOException e) {

			e.printStackTrace();
		}
		
		
		return super.doEndTag();
	}

	@Override
	public int doStartTag() throws JspException {
		return EVAL_BODY_AGAIN;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Map<String, String> getData() {
		return data;
	}

	public void setData(Map<String, String> data) {
		this.data = data;
	}

	public String getCurrentValue() {
		return currentValue;
	}

	public void setCurrentValue(String currentValue) {
		this.currentValue = currentValue;
	}

	@Override
	public int doAfterBody() throws JspException {
	 
		return EVAL_PAGE;
	}

	 

	 
	
	
	

}
